/**
 * 企业基本信息面板数据源
 * 
 * @class enterprsie_base_info_store
 * @extends Ext.data.Store
 */
enterprsie_base_info_benefit_store = Ext.extend(Ext.data.GroupingStore, {
	constructor : function() {
		// this.cfg = _cfg;
		enterprsie_base_info_benefit_store.superclass.constructor.call(this, {
			proxy : new Ext.data.HttpProxy({
				url : 'cxxqysjsb/menu_base_info/getallenterprsie_base_info.action?id='
						+ userId,// 用户的ID
				method : 'post'
			}),
			autoLoad : true,
			sortInfo : {
				field : 'benefit_year',
				direction : "DESC"
			},
			groupField : 'benefit_year',
			reader : new Ext.data.JsonReader({
						fields : ['benefit_id', 'enterprise_id', 'editable',
								'benefit_year', 'benefit_profit_tax',
								'benefit_profit',
								'benefit_major_business_lastyear_sale',
								'benefit_is_new', 'benefit_check_pass',
								'benefit_type']
					})
		})
	}
});
arrsm = new Ext.grid.RowSelectionModel({
			singleSelect : true
		});// 选择模型
/**
 * 数据显示面板
 * 
 * @class enterprsie_base_info_grid
 * @extends Ext.grid.GridPanel
 */
enterprsie_base_info_grid = Ext.extend(Ext.grid.GridPanel, {
	cfg : null,
	store : null,
	addnewwin : null,
	editwin : null,
	constructor : function() {
		this.cfg = {
			id : userId
		};
		this.addnewwin = new enterprsie_base_info_addnewbenefitpanel_win();
		this.store = new enterprsie_base_info_benefit_store();
		enterprsie_base_info_grid.superclass.constructor.call(this, {
			loadMask : {
				msg : '数据加载中...'
			},
			title : '企业基本情况-基本信息',
			iconCls : 'SMLPoolManage_sml',
			sm : arrsm,
			frame : true,
			view : new Ext.grid.GroupingView({
				forceFit : true,
				groupTextTpl : '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "条数据" : "条数据"]})'
			}),
			cm : new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
						header : 'ID',
						width : 10,
						dataIndex : 'benefit_id',
						id : 'benefit_id',
						hidden : true,
						align : 'center'
					}, {
						header : '企业编号',
						width : 10,
						dataIndex : 'enterprise_id',
						id : 'enterprise_id',
						hidden : true,
						sortable : false,
						align : 'center'
					}, {
						header : '年度',
						width : 100,
						dataIndex : 'benefit_year',
						sortable : false,
						renderer : this.getYearN,
						align : 'center'
					}, {
						header : '主营销售额',
						width : 120,
						dataIndex : 'benefit_major_business_lastyear_sale',
						sortable : false,
						renderer : this.getBenefitName,
						align : 'center'
					}, {
						header : '利润',
						width : 120,
						dataIndex : 'benefit_profit',
						sortable : false,
						renderer : this.getBenefitName,
						align : 'center'
					}, {
						header : '利税',
						width : 120,
						dataIndex : 'benefit_profit_tax',
						sortable : false,
						renderer : this.getBenefitName,
						id : 'benefit_profit_tax',
						align : 'center'
					}, {
						header : '类型',
						width : 160,
						dataIndex : 'benefit_type',
						renderer : this.getBenefitTypeName,
						id : 'benefit_type',
						align : 'center'
					}, {
						header : '审核状态',
						width : 100,
						dataIndex : 'benefit_check_pass',
						renderer : this.getCheckTypeName,
						id : 'benefit_check_pass',
						align : 'center'
					}]),
			autoExpandColumn : 'benefit_check_pass',
			tbar : [{
				text : '增加<font color="red">' + OPTION_NDDECLARE_YEAR + '年'
						+ getDataType(OPTION_DATA_TYPE) + '</font>数据',
				scope : this,
				iconCls : 'itemadd',
				handler : function() {
					this.addnewwin.show(Ext.getDom('west-div'));
				}
			}, '-', {
				text : '修改',
				scope : this,
				tooltip:'只允许修改草稿数据',
				iconCls : 'smlEdit_edit',
				handler : function() {
					var ro = this.getSelectionModel().getSelected();
					if (!ro) {
						Ext.Msg.show({
									title : '提示',
									msg : '没有选择数据',
									icon : Ext.MessageBox.WARNING,
									buttons : Ext.Msg.OK
								});
						return;
					} else {
						var benefit_id = ro.get('benefit_id');
						var year = ro.get('benefit_year');
						var type = ro.get('benefit_type');
						var benefit_major_business_lastyear_sale = ro
								.get('benefit_major_business_lastyear_sale');
						var benefit_profit = ro.get('benefit_profit');
						var benefit_profit_tax = ro.get('benefit_profit_tax');
						var benefit_check_pass = ro.get('benefit_check_pass');
						/**
						 * 历史数据不允许修改
						 */
						if (benefit_check_pass == 0) {
							Ext.Msg.show({
										title : '提示',
										msg : '正在审核的数据不能进行修改',
										icon : Ext.MessageBox.ERROR,
										buttons : Ext.Msg.OK
									});
							return;
						} else if (benefit_check_pass == 1
								|| benefit_check_pass == 2) {
							Ext.Msg.show({
										title : '提示',
										msg : '审核完成的数据不能进行修改',
										icon : Ext.MessageBox.ERROR,
										buttons : Ext.Msg.OK
									});
							return;
						} else {
							var cfg = {
								benefit_id : benefit_id,
								year : year,
								type : type,
								benefit_major_business_lastyear_sale : benefit_major_business_lastyear_sale,
								benefit_profit : benefit_profit,
								benefit_profit_tax : benefit_profit_tax
							}
							this.editwin = new enterprsie_base_info_editbenefitpanel_win(cfg);
							this.editwin.show(Ext.getDom('west-div'));
							this.editwin.on('addok', function() {
										this.getStore().reload();
									}, this);
						}
					}
				}
			},'-', {
				text : '刷新',
				scope : this,
				iconCls : 'refreshtty',
				handler : function() {
					this.getStore().reload();
				}
			}]

		});
		this.addnewwin.on('addok', function() {
					this.getStore().reload();
				}, this);

	},
	getYearN : function(value) {
		return value + '年';
	},
	getBenefitName : function(value) {
		return value + '万元';
	},
	getBenefitTypeName : function(code) {
		return '<font color="red">' + getDataType(code) + "</font>";
	},
	getCheckTypeName : function(code) {
		if (code == 0) {
			return '<font color="green">正在审核</font>';
		} else if (code == 1) {
			return '<font color="blue">审核通过</font>';
		} else if (code == 2) {
			return '<font color="red">审核不通过</font>';
		} else if (code == 3) {
			return '<font color="green">草稿状态</font>';
		}
	}
});
/**
 * 新增一条新的数据
 * 
 * @class enterprsie_base_info_addnewbenefitpanel
 * @extends Ext.form.FormPanel
 */
enterprsie_base_info_addnewbenefitpanel = Ext.extend(Ext.form.FormPanel, {
			constructor : function() {
				enterprsie_base_info_addnewbenefitpanel.superclass.constructor
						.call(this, {
									frame : true,
									height : 150,
									bodyStyle : 'padding: 10px 10px 0 10px;',
									labelWidth : 200,
									labelAlign : 'right',
									defaults : {
										anchor : '95%',
										allowBlank : true,
										regex : /^[0-9]+$/,
										msgTarget : 'side'
									},
									items : [{
										xtype : 'textfield',
										fieldLabel : '申报年度',
										anchor : '99%',
										name : 'type',
										readOnly : true,
										disabled : true,
										value : OPTION_NDDECLARE_YEAR + '年'
												+ getDataType(OPTION_DATA_TYPE)
												+ ''
									}, {
										xtype : 'hidden',
										name : 'benefittype',
										value : OPTION_DATA_TYPE
									}, {
										xtype : 'hidden',
										name : 'userid',
										value : userId
									}, {
										xtype : 'textfield',
										fieldLabel : '主营业务销售收入(万元)',
										anchor : '99%',
										name : 'benefit_major_business_lastyear_sale'
									}, {
										xtype : 'textfield',
										fieldLabel : OPTION_NDDECLARE_YEAR
												+ '企业经济效益利润(万元)',
										anchor : '99%',
										name : 'benefit_profit'
									}, {
										xtype : 'textfield',
										fieldLabel : OPTION_NDDECLARE_YEAR
												+ '企业经济效益利税(万元)',
										anchor : '99%',
										name : 'benefit_profit_tax'
									}]
								});
				/**
				 * 增加ok
				 */
				this.addEvents('addok');
				/**
				 * 删除ok
				 */
				this.addEvents('deleteok');
			},
			submit : function() {

			},
			reset : function() {
				this.getForm().reset();
			}
		});
enterprsie_base_info_addnewbenefitpanel_win = Ext.extend(Ext.Window, {
	formpanel : null,
	constructor : function() {
		this.formpanel = new enterprsie_base_info_addnewbenefitpanel;
		enterprsie_base_info_addnewbenefitpanel_win.superclass.constructor
				.call(this, {
					width : 500,
					title : '申报<font color="red">' + OPTION_NDDECLARE_YEAR
							+ '年' + getDataType(OPTION_DATA_TYPE) + '</font>数据',
					iconCls : 'itemadd',
					layout : 'fit',
					closeAction : 'hide',
					frame : true,
					modal : true,
					plain : true,
					items : this.formpanel,
					buttons : [{
								text : '保存草稿',
								scope : this,
								iconCls : 'itemadd',
								handler : this.submit
							}, {
								text : '取消',
								scope : this,
								iconCls : 'cancel',
								handler : function() {
									this.formpanel.reset();
									this.hide()
								}
							}]
				});
		/**
		 * 增加ok
		 */
		this.addEvents('addok');
		/**
		 * 删除ok
		 */
		this.addEvents('deleteok');
		this.formpanel.on('addom', function() {
					this.fireEvent('addok')
				}, this);
	},
	submit : function() {
		var form = this.formpanel.getForm();
		if (form.isValid()) {
			var benefit_major_business_lastyear_sale = form
					.findField('benefit_major_business_lastyear_sale')
					.getValue();
			var benefit_profit = form.findField('benefit_profit').getValue();
			var benefit_profit_tax = form.findField('benefit_profit_tax')
					.getValue();
			Ext.Ajax.request({
				url : 'cxxqysjsb/menu_base_info/addnewenterprsie_base_info_benefit.action',
				scope : this,
				success : function(response, options) {
					if (response.responseText == 'ok') {
						this.fireEvent('addok');
						this.hide();
					} else if (response.responseText == 'already') {
						Ext.Msg.show({
									title : '提示',
									msg : '<font color="red">'
											+ OPTION_NDDECLARE_YEAR + '年'
											+ getDataType(OPTION_DATA_TYPE)
											+ '</font>数据已经申报,请勿重复申报',
									icon : Ext.Msg.WARNING,
									buttons : Ext.Msg.OK
								});
						return;
					}
				},
				params : {
					userid : userId,
					year : OPTION_NDDECLARE_YEAR,
					type : OPTION_DATA_TYPE,
					benefit_major_business_lastyear_sale : benefit_major_business_lastyear_sale,
					benefit_profit : benefit_profit,
					benefit_profit_tax : benefit_profit_tax
				}
			})
		}
	}
})
// ////////////////////////
/**
 * 修改数据窗体
 * 
 * @class enterprsie_base_info_editbenefitpanel
 * @extends Ext.form.FormPanel
 */
enterprsie_base_info_editbenefitpanel = Ext.extend(Ext.form.FormPanel, {
	cfg : null,
	constructor : function(_cfg) {
		this.cfg = _cfg;
		enterprsie_base_info_editbenefitpanel.superclass.constructor.call(this,
				{
					frame : true,
					height : 150,
					bodyStyle : 'padding: 10px 10px 0 10px;',
					labelWidth : 200,
					labelAlign : 'right',
					defaults : {
						anchor : '95%',
						allowBlank : true,
						regex : /^[0-9]+$/,
						msgTarget : 'side'
					},
					items : [{
								xtype : 'hidden',
								name : 'id',
								value : this.cfg.benefit_id
							}, {
								xtype : 'hidden',
								name : 'benefittype',
								value : this.cfg.year
							}, {
								xtype : 'textfield',
								fieldLabel : '主营业务当年销售收入(万元)',
								anchor : '99%',
								value : this.cfg.benefit_major_business_lastyear_sale,
								name : 'benefit_major_business_lastyear_sale'
							}, {
								xtype : 'textfield',
								fieldLabel : OPTION_NDDECLARE_YEAR
										+ '年企业经济效益利润(万元)',
								anchor : '99%',
								value : this.cfg.benefit_profit,
								name : 'benefit_profit'
							}, {
								xtype : 'textfield',
								fieldLabel : OPTION_NDDECLARE_YEAR
										+ '年企业经济效益利税(万元)',
								anchor : '99%',
								value : this.cfg.benefit_profit_tax,
								name : 'benefit_profit_tax'
							}]
				})
	}
});
enterprsie_base_info_editbenefitpanel_win = Ext.extend(Ext.Window, {
	cfg : null,
	form : null,
	constructor : function(_cfg) {
		this.cfg = _cfg;
		this.form = new enterprsie_base_info_editbenefitpanel(this.cfg);
		enterprsie_base_info_editbenefitpanel_win.superclass.constructor.call(
				this, {
					width : 500,
					title : '修改<font color="red">' + this.cfg.year + '年'
							+ getDataType(this.cfg.type) + '</font>数据',
					iconCls : 'smlEdit_edit',
					layout : 'fit',
					closeAction : 'hide',
					frame : true,
					modal : true,
					plain : true,
					items : this.form,
					buttons : [{
								text : '保存',
								scope : this,
								iconCls : 'itemadd',
								handler : this.submit
							}, {
								text : '取消',
								scope : this,
								iconCls : 'cancel',
								handler : function() {
									this.hide()
								}
							}]
				})
	},
	submit : function() {
		var form = this.form.getForm();
		if (form.isValid()) {
			var id = form.findField('id').getValue();
			var year = form.findField('benefittype').getValue();
			var benefit_major_business_lastyear_sale = form
					.findField('benefit_major_business_lastyear_sale')
					.getValue();
			var benefit_profit = form.findField('benefit_profit').getValue();
			var benefit_profit_tax = form.findField('benefit_profit_tax')
					.getValue();
			Ext.Ajax.request({
				url : 'cxxqysjsb/menu_base_info/updateenterprsie_base_info_benefit.action?',
				scope : this,
				success : function(response, options) {
					if (response.responseText == 'ok') {
						this.fireEvent('addok');
						this.hide();
					}
				},
				params : {
					id : id,
					year : year,
					benefit_major_business_lastyear_sale : benefit_major_business_lastyear_sale,
					benefit_profit : benefit_profit,
					benefit_profit_tax : benefit_profit_tax
				}
			})
		}
	}
})
/**
 * 组装面板
 * 
 * @class
 * @extends demo.module
 */
Ext.extend(demo.module, {
			init : function() {

				this.datareport = new enterprsie_base_info_grid();
				this.main.add(this.datareport);
				this.main.doLayout();
			}
		});